<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="display" uri="http://displaytag.sf.net"%>
<%@ taglib prefix="indra" uri="http://indra.peru.com"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.widget.js"></script>

<script src="<%=request.getContextPath()%>/js/jquery/jquery-ui-1.8.13.custom.min.js"></script>



<script src="<%=request.getContextPath()%>/js/jquery/external/jquery.bgiframe-2.1.1.js"></script>

<script src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.mouse.js"></script> 
<script src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.draggable.js"></script> 
<script src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.resizable.js"></script> 
<script src="<%=request.getContextPath()%>/js/jquery/ui/jquery.ui.dialog.js"></script>

<!-- INI: MASCARA FORMATO -->
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery/external/jquery.metadata.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery/plugins/jquery.meio.mask.min.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculadora</title>
<style type="text/css">
	body{	 	
	    
	    color: #333333;
	    font-size: 11px;	    
	    text-transform:none;
		font-family: Arial,Helvetica,sans-serif;
		font-size: 11px;
	}

	input[type=Button] {
		  background-color: #E0EFFC;
		    border-color: #89B7D9;
		    border-style: solid groove groove solid;
		    border-width: 1px;
		    color: #1B4B70;
		    font-family: Arial,Helvetica,sans-serif;
		    font-size: 11px;
		    font-weight: bold;		    
		    text-transform: none;
		    width: 35px;
		    
	}
	input[type=text]{
		background: white!important;
		border: 1px solid #B1B1B1; 
		color: black;
		  
		 text-transform:none;
		 font-family: Arial,Helvetica,sans-serif;
		 font-size: 11px;
	}
	textarea{
		background: white!important;
		border: 1px solid #B1B1B1; 
		color: black;
		  
		 text-transform:none;
		 font-family: Arial,Helvetica,sans-serif;
		 font-size: 11px;
	}
	
	select{
	 	background: none repeat scroll 0 0 #ECECEC;
	    border: 1px solid #B1B1B1;
	    color: #333333;
	    font-size: 11px;
	    margin: 1px 0;
	}
	.calculadora{
		border-radius: 10px 10px 10px 10px;
	}

</style>
</head>
<BODY>
<CENTER>
<FORM name="Keypad" id="Keypad" onsubmit="return valida();">
<TABLE class="calculadora" border=2 width=50 height=60 cellpadding=1 cellspacing=5 style="text-align: center;">
<TR >
		<TD colspan=2 >
			<select name="selDivisa" id="selDivisa" class="editable" onchange="mostarCinta();">				
				<indra:options descripcion="descripcion" value="valoralfanumerico" tipoparametro="TIPO_DIVISAS"/>
			</select>
		</TD>
		<TD colspan=2 >
			<input type="button" value="Limpiar rollo" style="width: 85px!important" onClick="javascript:limpiarRodillo();" class="boton"/>
		</TD>
		<TD colspan=2 >
			
		</TD>
</TR>
<TR>
		<TD colspan=6>
<c:forEach items="${datos}" var="calculadora">
		<div style="display:none" id="${calculadora.codigoDivisa}">		
			<textarea id="txtHistorico${calculadora.codigoDivisa}" name="txtHistorico${calculadora.codigoDivisa}" 
			readonly="readonly" rows="6" style="width:210px;text-align:right;">${calculadora.operaciones}</textarea>
		</div>
</c:forEach>
		</TD>
</TR>
<TR>
	<TD colspan=3 >
		<input name="ReadOut" id="ReadOut" type="Text" value="0" width=100%  readonly="true">
	</TD>
	<TD>
		<input name="btnClear" type="Button" value="  C  " onClick="Clear()">
	</TD>
	<TD><input name="btnClearEntry" id="btnClearEntry" type="Button" value="  CE " onClick="ClearEntry()">
	</TD>
</TR>
<TR>
	<TD>
	<input name="btnSeven" id="btnSeven" type="Button" value="  7  " onClick="NumPressed(7)">
	</TD>
	<TD>
	<input name="btnEight" id="btnEight" type="Button" value="  8  " onClick="NumPressed(8)">
	</TD>
	<TD>
	<input name="btnNine" id="btnNine" type="Button" value="  9  " onClick="NumPressed(9)">
	</TD>
	<TD>
	<input name="btnNeg" id="btnNeg" type="Button" value=" +/- " onClick="Neg()">
	</TD>
	<TD>
	<input name="btnPercent" id="btnPercent" type="Button" value="  % " onClick="Percent()">
	</TD>
</TR>
<TR>
	<TD>
		<input name="btnFour" id="btnFour" type="Button" value="  4  " onClick="NumPressed(4)">
	</TD>
	<TD>
		<input name="btnFive" id="btnFive" type="Button" value="  5  " onClick="NumPressed(5)">
	</TD>
	<TD>
		<input name="btnSix" id="btnSix" type="Button" value="  6  " onClick="NumPressed(6)">
	</TD>	
	<TD >
		<input name="btnPlus" id="btnPlus" type="Button" value="  +  " onClick="Operation('+')">
	</TD>
	<TD >
		<input name="btnMinus" id="btnMinus" type="Button" value="   -   " onClick="Operation('-')">
	</TD>
</TR>
<TR>
	<TD>
		<input name="btnOne" id="btnOne" type="Button" value="  1  " onClick="NumPressed(1)">
	</TD>
	<TD>
		<input name="btnTwo" id="btnTwo" type="Button" value="  2  " onClick="NumPressed(2)">
	</TD>
	<TD>
		<input name="btnThree" id="btnThree" type="Button" value="  3  " onClick="NumPressed(3)">
	</TD>
	<TD >
		<input name="btnMultiply" id="btnMultiply"  type="Button" value="  *  " onClick="Operation('*')">
	</TD>
	<TD >
		<input name="btnDivide" id="btnDivide" type="Button" value="   /   " onClick="Operation('/')">
	</TD>
</TR>
	<TR>
	<TD>
		<input name="btnZero" id="btnZero" type="Button" value="  0  " onClick="NumPressed(0)">
	</TD>
	<TD>
		<input name="btnDecimal" id="btnDecimal" type="Button" value="   .  " onClick="Decimal()">
	</TD>
	<TD colspan=2>
		<input name="btnRetroceso" id="btnRetroceso"  style="width: 85px!important" type="Button" value="Retroceso" onClick="DeleteLast()">
	</TD>
	<TD>
		<input name="btnEquals" id="btnEquals" type="Button" value="  =  " onClick="Operation('=')">
	</TD>
</TR>
</TABLE>
<% 
String cerrar=request.getAttribute("cerrar")+"";
if(!cerrar.equals("0")){ %>
<div style="text-align: center; height: 50px;">
	<br/>
	<input type="button" value="Retornar" style="width: 85px!important" onClick="javascript:guardar();" class="boton"/>
</div>
<%}%>
</FORM>
</CENTER>

<SCRIPT LANGUAGE="JavaScript">

var FKeyPad = document.Keypad;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
var divisaActual="";
var divisaAnterior="";
function NumPressed (Num) {
	if (FlagNewNum) {
		FKeyPad.ReadOut.value  = Num;
		FlagNewNum = false;
	}else {
	if (FKeyPad.ReadOut.value == "0")
		FKeyPad.ReadOut.value = Num;
	else
		FKeyPad.ReadOut.value += Num;
    }
}
function Operation (Op) {
	var valorIngresado = FKeyPad.ReadOut.value; //LEE LA CAJA
	var valorReal=0;
	if(Op=='='&&PendingOp!=''){
		insertNewLine(valorIngresado + "   " + PendingOp  + "  ");
	}else{insertNewLine(valorIngresado + "   " + Op  + "  ");}
	
	
	FlagNewNum = true;
	if(Op=='+'||Op=='-'){ 
		valorReal=parseFloat(valorIngresado)*parseFloat(Op+'1');
		Accum=Math.round(Accum*100+valorReal*100)/100;		
	}else if(Op=='/'){
		valorReal=parseFloat(valorIngresado);		
		if(PendingOp!=''){Accum=Math.round((Accum/valorReal)*100)/100;}
		else{Accum=valorReal;}		
		PendingOp=Op;
	}else if(Op=='*'){
		valorReal=parseFloat(valorIngresado);
		if(PendingOp!=''){Accum = Math.round((Accum*valorReal)*100)/100;}
		else{Accum=valorReal;}		
		PendingOp=Op;		
	}else if(Op=='='){
		valorReal=parseFloat(valorIngresado);
		if(PendingOp=='/'){
			Accum=Math.round((Accum/valorReal)*100)/100;//Accum/=valorReal;
			insertNewLine(Accum + "   " + Op  + "  ");
			Accum=0;
		}else if(PendingOp=='*'){
			Accum = Math.round((Accum*valorReal)*100)/100;//Accum*=valorReal;
			insertNewLine(Accum + "   " + Op  + "  ");
			Accum=0;
		}else{Accum=0;}
		PendingOp='';	
			
	}	
	FKeyPad.ReadOut.value=parseFloat(Accum); 
		
	
	/*	
	if (FlagNewNum && PendingOp != "=");
	else{
		FlagNewNum = true;		
		if ( '+' == PendingOp )
			Accum += parseFloat(Readout);
		else if ( '-' == PendingOp )
			Accum -= parseFloat(Readout);
		else if ( '/' == PendingOp )
			Accum /= parseFloat(Readout);
		else if ( '*' == PendingOp )
			Accum *= parseFloat(Readout);
		else if ( '=' == PendingOp ){
			Accum = parseFloat(Readout);
			//insertNewLine(Accum + "   " + Op + "  ");
		}else{
			Accum = parseFloat(Readout);			
		}
		FKeyPad.ReadOut.value = Accum;
		if(Readout != FKeyPad.ReadOut.value){
			insertNewLine(FKeyPad.ReadOut.value + "   " + Op + "  ");
		}*/
		
}
function Decimal () {
	var curReadOut = FKeyPad.ReadOut.value;
	if (FlagNewNum) {
		curReadOut = "0.";
		FlagNewNum = false;
    }else{
		if (curReadOut.indexOf(".") == -1)
			curReadOut += ".";
   	}
	FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
	FKeyPad.ReadOut.value = "0";
	FlagNewNum = true;
}

function Clear () {
	Accum = 0;
	PendingOp = "";
	ClearEntry();
}
function Neg () {
	FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
	FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accum);
}

function DeleteLast () {	
	if(FKeyPad.ReadOut.value != "0"){	
		if(FKeyPad.ReadOut.value.length <= 1){
			FKeyPad.ReadOut.value = "0";
			FlagNewNum = true;
		}else{
			FKeyPad.ReadOut.value = FKeyPad.ReadOut.value.substring(0,FKeyPad.ReadOut.value.length-1);
		}
	}
}

document.onkeydown = f_keyDownHandlerCalc;

function f_keyDownHandlerCalc(evt){
	var evento = evt || window.event;	
	
	switch(evento.keyCode){
		//n umeros y punto decimal
		case 96: document.getElementById('btnZero').click(); break;
		case 97: document.getElementById('btnOne').click(); break;
		case 98: document.getElementById('btnTwo').click(); break;
		case 99: document.getElementById('btnThree').click(); break;
		case 100: document.getElementById('btnFour').click(); break;
		case 101: document.getElementById('btnFive').click(); break;
		case 102: document.getElementById('btnSix').click(); break;
		case 103: document.getElementById('btnSeven').click(); break;
		case 104: document.getElementById('btnEight').click(); break;
		case 105: document.getElementById('btnNine').click(); break;
		case 110: document.getElementById('btnDecimal').click(); break;
		
		case 48: document.getElementById('btnZero').click(); break;
		case 49: document.getElementById('btnOne').click(); break;
		case 50: document.getElementById('btnTwo').click(); break;
		case 51: document.getElementById('btnThree').click(); break;
		case 52: document.getElementById('btnFour').click(); break;
		case 53: document.getElementById('btnFive').click(); break;
		case 54: document.getElementById('btnSix').click(); break;
		case 55: document.getElementById('btnSeven').click(); break;
		case 56: document.getElementById('btnEight').click(); break;
		case 57: document.getElementById('btnNine').click(); break;
		case 190: document.getElementById('btnDecimal').click(); break;
		//operaciones
		case 8: document.getElementById('btnRetroceso').click(); break;
		case 111: document.getElementById('btnDivide').click(); break;
		case 106: document.getElementById('btnMultiply').click(); break;
		case 109: document.getElementById('btnMinus').click(); break;
		case 107: document.getElementById('btnPlus').click(); break;
		case 13: document.getElementById('btnEquals').click(); break;
		case 46: document.getElementById('btnClearEntry').click(); break;
		
		default: break;
	}	
}


window.onbeforeunload = function salida() {	
	guardar();
}

function insertNewLine(value){
	var textarea = document.getElementById('txtHistorico'+divisaActual);
	//var textArray = textarea.value.split("\n");
	textarea.value = textarea.value + value + "\n"
	textarea.scrollTop = textarea.scrollHeight;
	//var textArray = textarea.value	
}

function limpiarRodillo(){

var textarea = document.getElementById('txtHistorico'+divisaActual);
textarea.value ='';
Clear ();
}



function mostarCinta(){
	divisaAnterior=divisaActual;
	divisaActual=document.getElementById('selDivisa').value;	
	try{document.getElementById(divisaAnterior).style.display='none';}catch(e){}
	try{document.getElementById(divisaActual).style.display='';}catch(e){}
	var textArea = document.getElementById('txtHistorico'+divisaActual);
	textArea.scrollTop = textArea.scrollHeight;
	Clear ();
}
mostarCinta();

function guardar(){
			action = "operacion.do?method=guardarDatosCalculadora";
				$.ajax(
				{
					type:'POST',
					url:action,
					data: $('#Keypad').serialize(),					
					success: function(text){
						if(text!="1"){
							alert("Se presento un error en el sistema. Los datos no han sido grabados.");
						}	
						window.close();
					}	
				});
				
}


function valida(){
return false;
}

</SCRIPT>
</BODY>
</html>